<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\shop\controller;

use app\admin\model\MemberModel;
use cmf\controller\RestBaseController;
use app\admin\model\ReceiveAddressModel;
use api\index\controller\CommonController;
use think\Request;

class UserAddressesController extends RestBaseController
{
    protected $noNeedLogin = [];

    /**
     * 收获地址列表
     */
    public function index()
    {
        if (! $this->request->isGet()){
            $this->success('请求方式错误');
        }
        $member = MemberModel::get($this->getUserId());
        $address = $member->address()->order('is_default desc')->order('create_time desc')->paginate(config('verify.limit'));
        $this->success('成功', $address);

    }

    /**
     * 新建收获地址
     */
    public function store()
    {
        if (! $this->request->isPost()){
            $this->success('请求方式错误');
        }
        $data = $this->request->post();
        $this->checkValidate($data,config('check.address'));
        $member = MemberModel::get($this->getUserId());
        if(count($member->addresses) == 0){
            //第一次创建设为默认
            $data['is_default'] = true;
        }
        $data['member_id'] = $this->getUserId();
        if ($address = ReceiveAddressModel::create($data)){
             $this->success('保存成功', $address);
        }
        $this->success('保存失败', $address);
    }

    /**
     * @param ReceiveAddressModel $user_address
     * @param Request $request
     * 修改收获地址
     */
    public function update(Request $request)
    {
        if (! $this->request->isPut()){
            $this->success('请求方式错误');
        }
        $data = $this->request->put();
        $this->checkValidate($data,config('check.address'));

        if (! $address = ReceiveAddressModel::get($data['id'])){
            $this->error('修改失败，数据不存在');
        }

        if ($address->save($data)){
            $this->success('修改成功',$address);
        }
        $this->error('修改失败');
    }


    /**
     * @param $id
     * 删除收获地址
     */
    public function delete($id)
    {
        if (! $this->request->isDelete()){
            $this->success('请求方式错误');
        }
        if (! $data = ReceiveAddressModel::get($id)){
            $this->error('删除失败,数据不存在');
        }
        if ($data->member_id !== $this->getUserId()){
            $this->error('删除失败,该地址不属于此用户');
        }
        $data->delete();
        $this->success('删除成功');
    }

    /**
     * @param $id
     * 设置默认
     */
    public function setDefault(Request $request)
    {
        if (! $this->request->isPost()){
            $this->success('请求方式错误');
        }
        if (! $data = ReceiveAddressModel::get($request->id)){
            $this->error('数据不存在');
        }
        if ($data->member_id !== $this->getUserId()){
            $this->error('失败，该地址不属于此用户');
        }
        if (! $data->is_default){
            if($default = ReceiveAddressModel::where('is_default',ReceiveAddressModel::DEFAULT_ADDRESS)->find()){
                //修改之前默认地址
                $default->is_default = ReceiveAddressModel::NO_DEFAULT_ADDRESS ;
                $default->save();
            }
            $data->is_default = ReceiveAddressModel::DEFAULT_ADDRESS;
            $data->save();
        }

        $this->success('保存成功',$data);
    }

    //获取收货地址
    public function getAddress(){
        if($this->request->isGet()){
            $address_id = $this->request->get('address_id');
            $rule = config('verify.get_address');
            $this->checkValidate(['address_id'=>$address_id],$rule);

            $member_id = $this->getUserId();
            $res = CommonController::findWhereData('receive_address',['id'=>$address_id,'member_id'=>$member_id],'id,name,mobile,province,city,area,address');
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

    //获取默认收货地址
    public function getDefaultAddress(){
        if($this->request->isGet()){
            $member_id = $this->getUserId();
            $default = ReceiveAddressModel::DEFAULT_ADDRESS;
            $res = CommonController::findWhereData('receive_address',['member_id'=>$member_id,'is_default'=>$default],'id,name,mobile,province,city,area,address');
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

}
